<template>
  <div class="box">
    <div class="card-container">
      <el-row :gutter="10">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
          <el-col :span="19" :offset="0">
            <el-form-item label="设备型号" prop="model">
              <el-input v-model="queryParams.model" placeholder="请输入设备型号" clearable size="small"
                @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="设备类型" prop="type">
              <!-- <el-input v-model="form.type" placeholder="请输入描述" /> -->
              <el-select v-model="queryParams.type" :style="'width:215px;'" clearable placeholder="请选择设备类型">
                <el-option v-for="item in this.dict.type.sys_device_mode" :key="item.value" :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>

            <!-- <el-form-item label="设备厂商" prop="facId">
            <el-select v-model="queryParams.facId" :style="'width:215px;'" clearable placeholder="请选择设备厂商">
              <el-option v-for="(item, index) in options1" :key="index" :label="item.facturer" :value="item.facId">
              </el-option>
            </el-select>
          </el-form-item> -->
            <!-- <el-form-item label="是否启用" prop="status">
              <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
                <el-option v-for="dict in dict.type.sys_normal_disable" :key="dict.value"
                  :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item> -->
          </el-col>
          <el-col :span="5" :offset="0">
            <el-form-item>
              <el-button  icon="el-icon-search" size="mini" @click="handleQuery">搜索
              </el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
    </div>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button   icon="el-icon-plus" size="mini" v-hasPermi="['deviceMode:add']"
          @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button   icon="el-icon-delete" size="mini" :disabled="multiple"
          v-hasPermi="['deviceMode:remove']" v-show="!multiple" @click="handleDelete">删除</el-button>
      </el-col>
    </el-row> -->
    <div class="card-container">
      <div class="body-title">
        <div class="body-title-left">设备型号管理</div>
        <div class="body-title-right">
          <el-button icon="el-icon-plus" size="mini" v-hasPermi="['deviceMode:add']" @click="handleAdd">新增</el-button>
          <el-button   icon="el-icon-delete" size="mini" :disabled="multiple"
            v-hasPermi="['deviceMode:remove']" v-show="!multiple" @click="handleDelete">删除</el-button>
          <!-- <el-button  size="mini" :disabled="single" v-show="!multiple" @click="handleUpdate">
            修改</el-button> -->
          <!-- <el-button  size="mini" :disabled="multiple" v-hasPermi="['deviceMode:remove']" v-show="!multiple"
            @click="handleDelete">删除</el-button> -->
          <!-- <el-button  size="mini" v-show="!multiple" @click="handleExport">导出</el-button> -->
          <!-- <el-button   icon="el-icon-plus" size="mini" v-hasPermi="['deviceMode:add']"
            @click="handleAdd">新增
          </el-button> -->
        </div>
      </div>
      <el-table v-loading="loading" height="500" ref="table" :data="infoList" :row-key="getRowKeys"
        @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" :selectable="checkSelectable" :reserve-selection="true"
          align="center" />
        <el-table-column label="设备型号" align="center" prop="model" />
        <el-table-column label="型号版本" align="center" prop="version" />
        <el-table-column label="设备类型" align="center" prop="typeLabel" />
        <!-- <el-table-column label="设备厂商" align="center" prop="facturer" /> -->
        <el-table-column label="型号描述" align="center" prop="desc" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <!-- <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span> -->
            <span>{{ scope.row.createTime }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="是否启用" align="center" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
          </template>
        </el-table-column> -->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleUpdate(scope.row)" v-hasPermi="['deviceMode:edit']">
              编辑</el-button>
            <el-button size="mini" type="text" @click="handleDetails(scope.row)" v-hasPermi="['deviceMode:query']">
              详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改设备型号对话框 -->
    <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="500px" append-to-body>
      <div class="card-container" style="position: relative;">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="设备型号" prop="model">
            <el-input v-if="title !== '详情'" v-model="form.model" placeholder="请输入设备型号" />
            <span v-else>{{ form.model }}</span>
          </el-form-item>
          <el-form-item label="型号版本" prop="version">
            <el-input v-if="title !== '详情'" v-model="form.version" placeholder="请输入型号版本" />
            <span v-else>{{ form.version }}</span>
          </el-form-item>
          <el-form-item label="设备类型" prop="type">
            <!-- <el-input v-model="form.type" placeholder="请输入描述" /> -->
            <el-select v-if="title !== '详情'" v-model="form.type" :style="'width:340px;'" placeholder="请选择设备类型">
              <el-option v-for="item in this.dict.type.sys_device_mode" :key="item.value" :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            <span v-else>{{ form.typeLabel }}</span>
          </el-form-item>
          <!-- <el-form-item label="设备厂商" prop="facId">
          <el-select v-if="title !== '详情'" v-model="form.facId" :style="'width:380px;'" placeholder="请选择设备厂商">
            <el-option v-for="(item, index) in options1" v-show="item.status == '0'" :key="index" :label="item.facturer"
              :value="item.facId">
            </el-option>
          </el-select>
          <span v-else>{{ form.facturer }}</span>
        </el-form-item> -->
          <el-form-item label="型号描述" prop="desc">
            <el-input v-if="title !== '详情'" maxlength="200" v-model="form.desc" placeholder="请输入描述" />
            <span v-else>{{ form.desc }}</span>
          </el-form-item>
        </el-form>

      </div>
      <div slot="footer" class="dialog-footer" style="position: relative;">
        <el-button  v-if="title !== '详情'" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">{{
          title !== "详情" ? "取 消" : "关 闭"
        }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listInfo,
  getInfo,
  delInfo,
  addInfo,
  updateInfo,
  getManufacturer,
  getManufacturer1,
} from "@/api/deviceMode/deviceMode";
const numPass = (rule, value, callback) => {
  if (!/^[0-9a-zA-Z.]+$/.test(value)) {
    callback(new Error("请输入正确的格式"));
  } else {
    callback();
  }
};
export default {
  name: "DeviceMode",
  dicts: ["sys_normal_disable", "sys_device_mode"],
  data() {
    return {
      options: [],
      options1: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备型号表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        model: null,
        type: null,
        facId: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        model: [
          { required: true, message: "设备型号不能为空", trigger: "blur" },
        ],
        version: [
          {
            required: true,
            message: "型号版本不能为空",
            trigger: "blur",
          },
          { validator: numPass, trigger: "blur" },
        ],
        type: [
          { required: true, message: "设备类型不能为空", trigger: "change" },
        ],
        facId: [
          { required: true, message: "设备厂商不能为空", trigger: "change" },
        ],
        // status: [
        //   { required: true, message: '状态不能为空', trigger: 'change' },
        // ],
      },
    };
  },
  created() {
    this.getList();
    this.getFac();
    this.getFac1();
  },
  activated() {
    this.getList();
  },
  methods: {
    checkSelectable(row) {
      return row.useCount <= 0;
    },
    getRowKeys(row) {
      return row.modelId;
    },
    handleDetails(row) {
      this.reset();
      const modelId = row.modelId || this.ids;
      getInfo(modelId).then((response) => {
        this.form = response.data;
        this.getFac();
        this.getFac1();
        this.open = true;
        this.title = "详情";
      });
    },
    /** 查询设备型号列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then((response) => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        model: null,
        version: null,
        type: null,
        facId: null,
        desc: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.modelId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    async getFac() {
      let res = await getManufacturer();
      if (res.code === 200) {
        this.options = res.data;
      }
    },
    async getFac1() {
      let res = await getManufacturer1();
      if (res.code === 200) {
        this.options1 = res.data;
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getFac();
      this.open = true;
      this.title = "新增";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const modelId = row.modelId || this.ids;
      getInfo(modelId).then((response) => {
        // this.options
        this.form = response.data;
        this.getFac();
        this.getFac1();
        this.open = true;
        this.title = "编辑";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.modelId != null) {
            updateInfo(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const modelIds = row.modelId || this.ids;
      this.$modal
        .confirm("是否确认删除当前设备型号？")
        .then(function () {
          return delInfo(modelIds);
        })
        .then(() => {
          this.$refs.table.clearSelection();
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "deviceMode/info/export",
        {
          ...this.queryParams,
        },
        `info_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.box {
  width: 100%;
  height: 100%;
  padding: 20px;
}


.body-title {
  display: flex;
  position: relative;
  // justify-content: space-between;
  align-items: center;
}

.body-title-left {
  height: 100%;
  font-size: 16px;
  font-family: PingFangSC-Medium, PingFang SC;
  font-weight: 600;
  color: #fff;
  line-height: 64px;
}

.body-title-right {
  position: absolute;
  right: 0;
}

span {
  color: rgba(255, 255, 255, .6);
}
</style>
